package com.ustc.hewei.rpc.server.register;

import com.ustc.hewei.rpc.server.ServiceObject;
import lombok.extern.slf4j.Slf4j;

import java.net.UnknownHostException;
import java.util.HashMap;

/**
* @description: 将服务注册到服务端本地的map集合中，以便之后客户端的调用
* @author hewei
* @date 2022/7/26 19:08
* @version 1.0
*/

@Slf4j
public class DefaultServiceRegister implements ServiceRegister{

    //服务端存放已经注册的服务的集合
    private HashMap<String, ServiceObject> ServiceMap = new HashMap<>();

    //服务协议，供以后拓展
    public String protocol;

    //提供服务的端口
    public int port;

    @Override
    public void register(ServiceObject service) throws UnknownHostException {
        if (service == null) {
            log.error("注册服务为空，注册失败！");
            return;
        }
        ServiceMap.put(service.getName(), service);
    }

    @Override
    public ServiceObject getServiceObject(String name) {
        ServiceObject serviceObject = ServiceMap.get(name);
        if (serviceObject == null) {
            log.error("没有所要调用的服务{}！", name);
            throw new RuntimeException("服务端并无相应服务！");
        } else {
            return serviceObject;
        }
    }
}
